package com.example.sjly.ui.scheduling;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sjly.MainActivity;
import com.example.sjly.R;
import com.example.sjly.sqliteroom.AppRoomDatabase;

import com.example.sjly.ui.scheduling.room.TOrderMess;
import com.example.sjly.ui.scheduling.room.TOrderRoomDatabase;


public class SchedulingDatamess extends AppCompatActivity {
    private Spinner spinnerButton1;//课程类型
    private Spinner spinnerButton2;//建设类型
    private Spinner spinnerButton3;//版本类型

    CalendarView calendarView;
    int calendarchoicedYear;
    int calendarchoicedMonth;
    int calendarchoicedDay;

    public String courseType;
    public String constructionType;
    public String versionType;

    private int flag=0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_scheduling_datamessage);


        //关于spinner
        spinnerButton1 = (Spinner) findViewById(R.id.coursespinner);
        ArrayAdapter<CharSequence> adapter =
                ArrayAdapter.createFromResource(this, R.array.coursetype, android.R.layout.simple_spinner_item);
        //设置spinner中每个条目的样式，同样是引用android提供的布局文件
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerButton1.setAdapter(adapter);
        spinnerButton1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                //将选择的元素显示出来
                String selected = parent.getItemAtPosition(position).toString();
                courseType=selected;

            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                System.out.println("nothingSelect");
            }
        });


        spinnerButton2 = (Spinner) findViewById(R.id.constructionspinner);
        ArrayAdapter<CharSequence> adapter2 =
                ArrayAdapter.createFromResource(this, R.array.constructiontype, android.R.layout.simple_spinner_item);
        //设置spinner中每个条目的样式，同样是引用android提供的布局文件
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerButton2.setAdapter(adapter2);
        spinnerButton2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                //将选择的元素显示出来
                String selected = parent.getItemAtPosition(position).toString();
                constructionType=selected;
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                System.out.println("nothingSelect");
            }
        });



        spinnerButton3 = (Spinner) findViewById(R.id.versionspinner);
        ArrayAdapter<CharSequence> adapter3 =
                ArrayAdapter.createFromResource(this, R.array.versiontype, android.R.layout.simple_spinner_item);
        //设置spinner中每个条目的样式，同样是引用android提供的布局文件
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerButton3.setAdapter(adapter3);
        spinnerButton3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                //将选择的元素显示出来
                String selected = parent.getItemAtPosition(position).toString();
                versionType=selected;
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                System.out.println("nothingSelect");
            }
        });



        //关于日历选择
        calendarView = (CalendarView) findViewById(R.id.calendarView);
        //calendarView 监听事件
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange( CalendarView view, int year, int month, int dayOfMonth) {
                //显示用户选择的日期
                int Month;
                Month=month+1;
                calendarchoicedYear=year;
                calendarchoicedMonth=Month;
                calendarchoicedDay=dayOfMonth;
                Toast.makeText(SchedulingDatamess.this,year + "年" + Month+ "月" + dayOfMonth + "日",Toast.LENGTH_SHORT).show();
            }
        });

        

        //确认按钮
        findViewById(R.id.scheduing_nextstep1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    TOrderRoomDatabase CTM = TOrderRoomDatabase.getInstance(getApplicationContext());
                    TOrderMess ctm = new TOrderMess(courseType, constructionType, versionType, calendarchoicedYear, calendarchoicedMonth, calendarchoicedDay, 1);
                    CTM.torderDAO().insert(ctm);

             Toast.makeText(SchedulingDatamess.this,"保存成功",Toast.LENGTH_SHORT).show();


            }
        });


    }




}

